package collision;


public class Missile extends Thing{

	private double dX = 1, dY = 1;
    private double ddy;
    private Vector2D dir;
    private int fireHeight;
    private static String iiString = "bird.png";
    private boolean hitGround;
    private int startTime;


    private final int BOARD_WIDTH = 700;

    /**
     * Missile Constructor
     *
     * @param  x  initial position x
     * @param  y  initial position y
     * @param  direction  vector representing initial direction of the missile with the magnitude set as the speed.
     */
    public Missile(int x, int y, Vector2D direction) 
    {
    	super(x, y, iiString);
    	fireHeight = y+15;
    	hitGround = false;
    	startTime = -1;
        ddy=.1;
        dir = direction;
        dX = dir.x;
        dY = dir.y;
    }
    
    /**
     * Returns the vecotr representing the current direction and orientation of this missile.
     * 
     * @return	the vector representing this missile.
     */
    public Vector2D getVector()
    {
    	return dir;
    }
    
    /**
     * The initial height this missile was fired at.
     * 
     * @return  The initial height in pixels this missile was fired at.
     */
    public int getFireHeight()
    {
    	return fireHeight;
    }
    
    /**
     * Stops this missile.  When it hits the ground.
     */
    public void stop()
    {
    	dX = 0;
    	dY = 0;
    }
    
    /**
     * Weather or not this missile has hit the ground yet.
     * 
     * @return  Weather or not this missile has hit the ground yet.
     */
    public boolean hitGround()
    {
    	if(hitGround)
    	{
    		int t = (int) (System.currentTimeMillis()-startTime);
    		if(startTime == -1)
    			startTime = (int)System.currentTimeMillis();
    		if(t > 1000)
    			super.setVisible(false);

    	}
    	return hitGround;
    }
    
    /**
     * Overwrites the Shootable move method to include acceleration and gravity for the missile.
     * This method is run for each frame of the game.
     */
    public void move() 
    {
    	int x = getX();
    	int y = getY();
        x += dX;
        dY = dY + ddy;
        y += (int)dY;
        if (x > BOARD_WIDTH || y > 500)
            setVisible(false);
        setPosition(x,y); 
        if(!hitGround)
        {
        	dir = new Vector2D(dX,dY);
        	dir = dir.normalize();
        }
        if(super.getY()>=fireHeight)
    		hitGround = true; 

    }
}
